<!--打印-->
<template>
  <el-dialog append-to-body title="打印机设置" v-if="printInfoModel" :visible.sync="printInfoModel" width="35%" @close="close()">
    <avue-form ref="printForm" v-model="printInfo" :option="printInfoOption"> </avue-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" @click="printSubmit"><i class="el-icon-printer"></i><span>打 印</span></el-button>
      <el-button size="small" @click="printInfoModel = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
    </span>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getData" :key="timer" @newKey="newKey"></common-modal-template>
  </el-dialog>
</template>
<script>
import { print } from '@/api/warehouse/warehouseManagement';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
export default {
  name: 'printDialog',
  components: {
    CommonModalTemplate
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  mounted() {},
  data() {
    return {
      printInfo: {},
      printInfoOption: {
        labelWidth: 150,
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '打印机',
            prop: 'equipmentName',
            span: 20,
            type: 'search',
            placeholder: '请选择 打印机',
            disabled: true,
            rules: [
              {
                required: true,
                message: '请选择打印机',
                trigger: 'blur'
              }
            ],
            appendClick: () => {
              this.openPrintersModal();
            }
          },
          {
            label: '标签模版',
            prop: 'labelTemplateName',
            span: 20,
            type: 'search',
            placeholder: '请选择 标签模板',
            disabled: true,
            rules: [
              {
                required: true,
                message: '请选择标签模板',
                trigger: 'blur'
              }
            ],
            appendClick: () => {
              this.openLabelTemplateModal();
            }
          }
        ]
      },
      printInfoModel: false,
      tableOption: {},
      modalName: '',
      timer: new Date().getTime()
    };
  },
  watch: {
    visible: {
      handler(value) {
        this.printInfoModel = value;
      }
    }
  },
  computed: {},
  methods: {
    newKey() {
      this.timer = new Date().getTime();
    },
    handlePrint(row) {
      this.printInfo.id = row.id;
      this.printInfo.name = row.name;
      this.printInfo.code = row.code;
      this.printInfoModel = true;
    },
    /*
     * 打印
     */
    printSubmit() {
      this.$refs.printForm.validate((valid, done) => {
        if (valid) {
          let params = {
            name: this.printInfo.name,
            code: this.printInfo.code,
            id: this.printInfo.id,
            printId: this.printInfo.equipmentId,
            labelTemplateId: this.printInfo.labelTemplateId
          };
          print(params).then(
            (res) => {
              this.$refs.printForm.resetFields();
              this.printInfoModel = false;
              this.$message({
                type: 'success',
                message: res.data.msg
              });
              done();
            },
            (error) => {
              done();
            }
          );
        }
      });
    },
    // 获取打印机
    openPrintersModal() {
      this.modalName = 'printersModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择打印机（双击进行选择）',
        url: '/api/mes-product/ems-equipment/page?type=PRINTER',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '打印机编码',
            prop: 'code',
            search: true,
            searchLabelWidth: 110
          },
          {
            label: '打印机名称',
            prop: 'name',
            search: true,
            searchLabelWidth: 110
          }
        ]
      };
    },
    // 获取标签模板
    openLabelTemplateModal() {
      this.modalName = 'labelTemplateModal';
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择标签模版（双击进行选择）',
        url: '/api/mes-admin/labeltemplate/page?status=1',
        isPage: true,
        rowSelectionType: 'single',
        columnDefs: [
          {
            label: '标签名称',
            prop: 'fileName',
            search: true
          },
          {
            label: '标签代码',
            prop: 'code',
            search: true
          },
          {
            label: '版本名称',
            prop: 'revision'
          },
          {
            label: '缩略图',
            prop: 'thumbnailUrl'
          },
          {
            label: '打印标签',
            prop: 'fileUrl'
          }
        ]
      };
    },
    getData(rowData) {
      switch (this.modalName) {
        case 'labelTemplateModal':
          this.$set(this.printInfo, 'labelTemplateId', rowData.id);
          this.$set(this.printInfo, 'labelTemplateName', rowData.fileName);
          break;
        case 'printersModal':
          this.$set(this.printInfo, 'equipmentId', rowData.id);
          this.$set(this.printInfo, 'equipmentName', rowData.name);
          break;
      }
    },
    close() {
      this.printInfoModel = false;
      this.$emit('closeDialog');
    }
  }
};
</script>
<style lang="scss" scoped></style>
